package com.yhq.basic.thread.lock;

import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @author hqyin
 * @date 2025/5/20 1:51 PM
 */
public class ExclusiveOwnerSelfLock extends AbstractQueuedSynchronizer {
    @Override
    protected boolean tryAcquire(int arg) {
        if(compareAndSetState(0,arg)) {
            setExclusiveOwnerThread(Thread.currentThread());
            return true;
        }
        return false;
    }

    @Override
    protected boolean tryRelease(int arg) {
        setExclusiveOwnerThread(null);
        setState(arg);
        return true;
    }

    public void lock() {
        acquire(1);
    }


    public void unlock() {
        release(0);
    }



}
